/*
----------------------------------------------------------------------------
Copyright (c) 2005 - 2009, Wangxing SHI	<swxlion@gmail.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above
	  copyright notice, this list of conditions and the
	  following disclaimer.

    * Redistributions in binary form must reproduce the above
	  copyright notice, this list of conditions and the following
	  disclaimer in the documentation and/or other materials
	  provided with the distribution.

    * Neither the copyright holder's name nor the names of its
	  contributors may be used to endorse or promote products
	  derived from this software without specific prior written
	  permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------
*/

/*----------------------------- Utility.cpp -------------------------------*/
/*===========================================================================
FILE: Utility.cpp
Author: Wangxing SHI (swxlion)
Version: 0.0.1
Date: 2009-06-20

File Description:
    Utility functions.

Dependencies:
    Platform: Platform Independent.

History:
    version        author        date       description
	----------------------------------------------------
	0.0.1          swxlion       09-06-20   Created the first version.
===========================================================================*/
#include "Utility.hpp"

/*===============================================================================
FUNCTION DEFINITIONS: Utility Functions
=============================================================================== */
/*===========================================================================

FUNCTION: LunaLog::Utility::EndianJudge

DESCRIPTION:
  Judge endian.

PARAMETERS:
	None.

RETURN VALUE:
   false - Big-endian;
   true - Little-endian.
===========================================================================*/
bool LunaLog::Utility::EndianJudge()
{
	short			data = 0x1234;
	unsigned char	*arr = (unsigned char*)(&data);

	if(arr[0] == 0x12)
		return false;
	else
		return true;
}

/*===========================================================================

FUNCTION: LunaLog::Utility::GetLocalTime

DESCRIPTION:
  Calculate the local time & date.

PARAMETERS:
	time [in] - The original time data return by time().
	timezone [in] - Time zone. e.g. The Beijing time zone is +8.
	tm_time [out] - The tm structure contain the local time & date.

RETURN VALUE:
   None.

NOTICE:
   1. The function localtime() declared in "time.h" isn't thread safety.
   2. Follow the practice of localtime() in "time.h", the returned year need to be added 1900 for the current year;
      and the returned month need to be added 1 for the current month.
   3. This function modified form Internet. And the original author is unknown. For the know, please inform.
===========================================================================*/
void LunaLog::Utility::GetLocalTime(time_t time, time_t timezone, struct tm *tm_time)
{
	const char Days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

	unsigned  int uiLeapYearNumber;
	int iHourPerYear;

	//-- Calculate the time difference.
	time = time + timezone * 3600;		//-- Verify the time zone. 1 hour = 3600 seconds.

	if(time < 0)
	{
		time = 0;
	}
	//-- Calculate the seconds.
	tm_time->tm_sec = (int)(time % 60);
	time /= 60;
	//-- Calculate the minutes.
	tm_time->tm_min = (int)(time % 60);
	time /= 60;
	//-- Calculate the number of leap year in the past. There is a leap year every four years. 4 years = 1461 * 24 hours.
	uiLeapYearNumber = ((unsigned int)time / (1461L * 24L));
	//-- Calculate the years.
	tm_time->tm_year = (uiLeapYearNumber << 2) + 70;		//-- 1970
	//-- Calculate the hours remained in four years.
	time %= 1461L * 24L;
	//-- Verify the year affected by leap years, and calculate the hours remained in a year.
	for (;;)
	{
		//-- 1 year = 365 * 24 hours.
		iHourPerYear = 365 * 24;
		//-- Leap year judgement.
		if ((tm_time->tm_year & 3) == 0)
		{
			//-- It is leap year which contain a special day.
			iHourPerYear += 24;
		}
		if (time < iHourPerYear)
		{
			break;
		}
		tm_time->tm_year++;
		time -= iHourPerYear;
	}
	//-- The hours.
	tm_time->tm_hour = (int)(time % 24);
	//-- The days remaining in a year.
	time /= 24;
	//-- Assume it is a leap year.
	time += 1;
	//-- Adjust the error for leap year, and calculate the month and day.
	if ((tm_time->tm_year & 3) == 0)
	{
		if (time > 60)
		{
			time--;
		}
		else
		{
			if (time == 60)
			{
				tm_time->tm_mon = 1;
				tm_time->tm_mday = 29;
				return ;
			}
		}
	}
	//-- Calculate the months & days.
	for (tm_time->tm_mon = 0; Days[tm_time->tm_mon] < time;tm_time->tm_mon++)
	{
		time -= Days[tm_time->tm_mon];
	}

	tm_time->tm_mday = (int)(time);
}
